package com.liujun.io.netty.seven.serializable.server;

import com.liujun.io.netty.seven.serializable.bean.SubscribeReq;
import com.liujun.io.netty.seven.serializable.bean.SubscribeResp;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class SubReqServerHandler extends ChannelHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		
		SubscribeReq req=(SubscribeReq) msg;
		
		if("liujun".equalsIgnoreCase(req.getUserName()))
		{
			System.out.println("Service accept client subscribe req: [ "+req+"]");
			ctx.writeAndFlush(resp(req.getSubReqID()));
		}
		
	}
	
	private SubscribeResp resp(int subReqID)
	{
		SubscribeResp resp = new SubscribeResp();
		
		resp.setSubReqID(subReqID);
		resp.setRespCode(0);
		resp.setDesc("Netty book order success, 3 days latter, sent to the designated address");
		
		return resp;
		
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		cause.printStackTrace();
		ctx.close();
	}

	
	
}
